/*************************************************************************
	> File Name: lockGuard.hpp
	> Author: H.M. Lau
	> Mail: hm.lau@outlook.com
	> Created Time: 2023/09/26 09:27
 ************************************************************************/

#ifndef _LOCKGUARD_H
#define _LOCKGUARD_H
#include "comm.hpp"

class Mutex
{
public:
    Mutex (pthread_mutex_t *mtx) : _pmtx(mtx)
    {}
    
    void lock()
    {
        std::cout << "It's going to lock." << std::endl;
        pthread_mutex_lock(_pmtx);
    }

    void unlock()
    {
        std::cout << "It's going to unlock." << std::endl;
        pthread_mutex_unlock(_pmtx);
    }

    virtual ~Mutex ()
    {}
private:
    pthread_mutex_t *_pmtx;
};

class LockGuard
{
public:
    LockGuard (pthread_mutex_t *mtx) : _mtx(mtx)
    {
        _mtx.lock();
    }

    virtual ~LockGuard ()
    {
        _mtx.unlock();
    }

private:
    Mutex _mtx;
};
#endif
